package dcsinstnce.server.domaines.impl;

import java.awt.BorderLayout;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

import dcsinstnce.server.domaines.LocalDomaineManager;
import dcsinstnce.server.domaines.LocalDomaineManagerOperations;
import dcsinstnce.server.interfaces.Suscriber;
import dcsinstnce.server.interfaces.impl.SuscriberImpl;
import dcsinstnce.server.interfaces.impl.SuscriberManagerImpl;
import dcsinstnce.server.topic.impl.TopicImpl;


public class LocalDomaineManagerImpl implements LocalDomaineManagerOperations {

	private Hashtable<String, TopicImpl> topics;
	private SuscriberManagerImpl suscriber_manager;
	JFrame frame = new JFrame();
	JFrame frame_debug = new JFrame();
	JTextArea area = new JTextArea();
	/**
	 * constructeur
	 */
	public LocalDomaineManagerImpl() {
		frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(400, 400);
		frame.setLayout(new BorderLayout());
		frame.setVisible(true);
		
		frame_debug.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame_debug.setSize(700, 400);
		frame_debug.setLayout(new BorderLayout());
		frame_debug.setLocation(((int)frame.getLocation().getX()) + 450, ((int)frame.getLocation().getY()));
		frame_debug.setVisible(true);
		frame_debug.getContentPane().add(area);

		topics = new Hashtable<String, TopicImpl>();
		suscriber_manager = new SuscriberManagerImpl();
	}
	/**
	 * 
	 * @param topic : le topic à ajouter
	 */
	public void addTopic(TopicImpl topic){
		topic.setDomaine(this);
		topic.setsuscriber((SuscriberImpl)getSuscriberManager().addNewDataReaderListener(
				topic.getName(),
				topic.getDataReader())
			);
		topics.put(topic.getName(), topic);
		
		showFrame();
	}
	/**
	 * 
	 * @return : le gestionnaire des subscribers
	 */
	public SuscriberManagerImpl getSuscriberManager() {
		return suscriber_manager;
	}
	/**
	 * affichage pour vérification
	 */
	public void showFrame() {DefaultMutableTreeNode node = new DefaultMutableTreeNode("domain");
		Set<String> keys = topics.keySet();
		Iterator<String> strings = keys.iterator();
		while (strings.hasNext()) {
			String x = strings.next();
			DefaultMutableTreeNode node1 = topics.get(x).createNode();
			node.add(node1);
		}
		JTree tree = new JTree(node);
		JPanel panel = (JPanel) frame.getContentPane();
		if (panel.getComponentCount() != 0)
			panel.remove(panel.getComponentCount() - 1);
		panel.setLayout(new BorderLayout());
		panel.invalidate();
		panel.add(tree);
		for (int i = 0; i < tree.getRowCount(); i++) {
			tree.expandRow(i);
		}
		panel.validate();
		panel.repaint();
		
		showDebug();
	}
	/**
	 * affichage de la fenetre de debugage
	 */
	public void showDebug(){
		for (int i = 0; i < suscriber_manager.getSuscriberCount(); i++) {
			area.append("suscriber" + i + " : "
					+ suscriber_manager.getSuscribers().get(i).getListenerCount());
			for (int j = 0; j < suscriber_manager.getSuscribers().get(i).getListenerCount(); j++) {
				area.append(" " +
						suscriber_manager.getSuscribers().get(i).getTopicName((short)j));
			}
			area.append("\n");
		}
	}

	@Override
	public Suscriber[] getTopicsSuscribers(String topicName,
			LocalDomaineManager sender) {
		int x = topicName.indexOf(".");
		if (x < 0) {
			TopicImpl topic = topics.get(topicName);
			if (topic == null)
				return new Suscriber[] { null };
			return new Suscriber[] { topic.getSuscriber() };
		}
		String topicN = topicName.substring(0, x);
		TopicImpl topic = topics.get(topicN);
		if (topic == null)
			return new Suscriber[] { null };
		Suscriber suscriber = topic.getSuscriberWithPath(topicName.substring(
				x + 1, topicName.length()));
		if (suscriber == null)
			return new Suscriber[] { null };
		return new Suscriber[] { suscriber };
	}
}
